package com.desktopmng.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import com.desktopmng.core.support.Pager;
import com.desktopmng.exception.JFossException;
import com.desktopmng.orm.DstcToRole;

@SuppressWarnings({"unchecked","rawtypes"})
public class DstcToRoleManager extends HibernateEntityDao<DstcToRole> {
	/**
	 * Description: 新增桌面内容与角色关联
	 * 
	 * @param dstcToRole
	 *            桌面内容与角色关联对象
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String addDstcToRole(DstcToRole dstcToRole) throws JFossException {
		String retStr = "true";
		try {
			dstcToRole.setId(null);
			this.save(dstcToRole);
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}

	/**
	 * Description: 修改桌面内容与角色关联
	 * 
	 * @param dstcToRole
	 *            桌面内容与角色关联对象
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String updateDstcToRole(DstcToRole dstcToRole) throws JFossException {
		String retStr = "true";
		try {
			this.getHibernateTemplate().update(dstcToRole);
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}

	/**
	 * Description: 删除桌面内容与角色关联
	 * 
	 * @param dstcToRoleId
	 *            桌面内容与角色关联对象Id
	 * @return 操作标记
	 * @throws JFossException
	 */
	public String deleteDstcToRole(Long dstcToRoleId) throws JFossException {
		String retStr = "true";
		try {
			Properties prop = new Properties();
			prop.setProperty("roleId", dstcToRoleId.toString());
			List paramList;

			paramList = this.findDstcToRoleByCondition(prop);
			if (paramList!=null) {
				for (int i = 0; i < paramList.size(); i++) {
					DstcToRole dst = (DstcToRole) paramList.get(i);
					System.out.println(dst.getDstcId());
					this.getHibernateTemplate().delete(dst);
				}
			}
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return retStr;
	}

	/**
	 * Description: 查看桌面内容与角色关联对象
	 * 
	 * @param dstcToRoleId
	 *            桌面内容与角色关联对象Id
	 * @return 桌面内容与角色关联对象
	 * @throws JFossException
	 */
	public DstcToRole findDstcToRoleById(Long dstcToRoleId)
			throws JFossException {
		DstcToRole obj = null;
		try {
			if (null != dstcToRoleId) {
				obj = (DstcToRole) this.getHibernateTemplate().load(
						DstcToRole.class, dstcToRoleId.longValue());
			} else {
				return null;
			}
		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return obj;
	}

	/**
	 * Description: 条件查询桌面内容与角色关联库
	 * 
	 * 条件参数列表，为空时，返回全部桌面内容与角色关联列表
	 * 
	 * @param prop
	 *            条件参数表
	 * @return 封装桌面内容与角色关联库对象的列表
	 * @throws JFossException
	 */
	public List findDstcToRoleByCondition(Properties prop)
			throws JFossException {
		List list = null;
		try {

			String hql = "select p from DstcToRole p where 1=1";
			List paramList = new ArrayList();
			if (null != prop && prop.size() > 0) {
				String userId = prop.getProperty("roleId");
				if (StringUtils.isNotBlank(userId)) {
					hql += " and p.roleId = ?";
					paramList.add(new Long(userId));
				}
			}
			if (null != prop && prop.size() > 0) {
				String dstcId = prop.getProperty("dstcId");
				if (StringUtils.isNotBlank(dstcId)) {
					hql += " and p.dstcId = ?";
					paramList.add(new Long(dstcId));
				}
			}
			hql += " order by p.id";
			list = this.find(hql, paramList.toArray());

		} catch (Exception e) {

			throw new JFossException("", e);
		}

		if (list.isEmpty()) {
			return null;
		} else {
			return list;
		}
	}

	/**
	 * Description: 根据查询条件获取桌面内容与角色关联库对象列表,分页查询
	 * 
	 * @param prop
	 *            查询条件
	 * @param pageNum
	 *            页码
	 * @param pageSize
	 *            页数据量
	 * @return 桌面内容与角色关联库对象列表分页对象
	 * @throws JFossException
	 */
	public Pager findDstcToRoleByCondition(Properties prop, int pageNum,
			int pageSize) throws JFossException {
		Pager page = null;
		try {

			String hql = "select p from DstcToRole p where 1=1";
			List paramList = new ArrayList();
			if (null != prop && prop.size() > 0) {
				String msStatus = prop.getProperty("userId");
				if (StringUtils.isNotBlank(msStatus)) {
					hql += " and p.userId = ?";
					paramList.add(msStatus);
				}
			}
			hql += " order by p.id";
			page = this.pagedQuery(hql, pageNum, pageSize, paramList.toArray());

		} catch (Exception e) {

			throw new JFossException("", e);
		}
		return page;
	}

}
